﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.UI;
using Hr.BLL;
using Hr.Model;
using HR.HrHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HR.Areas.li_jialong.Controllers.Finance
{
    public class FinanceController : ApiController
    {
        //导入BLL层
        protected FinanceService Finance = new FinanceService();
        int code = 0;//0是成功，1是失败
        string msg = "success";//信息


        /// <summary>
        /// 获取所账号信息API
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetMailAccountLInfo(int page, int limit)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetMailAccountL(page,limit)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取所财务信息API
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetMailAccountInfo(int page, int limit)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetMailAccount(page,limit)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 添加、Finance_Account修改薪资信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateFinanceList()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            Finance_Account r = JsonHelper.DeserializeJsonToObject<Finance_Account>(RequestForm["AccountEditInfo"]);
            string type = RequestForm["Type"];
            int pk_User = int.Parse(RequestForm["pk_User"].ToString());
            if (type.Equals("Edit"))
            {
                Finance_Account SelRo = Finance.GetFinanceInfo(r.pk_Account);
                msg = "更新成功";
                if (SelRo == null)
                {
                    code = 1;
                    msg = "财务信息不存在";
                }
                else
                {
                    if (ModelHelper.ReflectModel(r, SelRo))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        if (!Finance.UpdateFinanceList(r, type,pk_User))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                Finance_Account SelRo = Finance.GetFinanceInfo(r.pk_Account);
                msg = "添加成功";

                if (SelRo != null)
                {
                    code = 1;
                    msg = "财务信息已存在！";
                }
                else
                {
                    if (!Finance.UpdateFinanceList(r, type,pk_User))
                    {
                        code = 1;
                        msg = "添加失败";
                    }
                }

            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 添加Finance_List修改数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateFinancelinsList()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            Finance_List r = JsonHelper.DeserializeJsonToObject<Finance_List>(RequestForm["FinanceEditInfo"]);
            string type = RequestForm["Type"];
            int pk_User = int.Parse(RequestForm["pk_User"].ToString());
            if (type.Equals("Edit"))
            {
                Finance_List SelRo = Finance.GetFinancelinsInfo(r.pk_Finance);
                msg = "更新成功";
                if (SelRo == null)
                {
                    code = 1;
                    msg = "财务信息不存在";
                }
                else
                {
                    if (ModelHelper.ReflectModel(r, SelRo))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        if (!Finance.UpdateFinance(r, type,pk_User))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                Finance_List SelRo = Finance.GetFinancelinsInfo(r.pk_Finance);
                msg = "添加成功";

                if (SelRo != null)
                {
                    code = 1;
                    msg = "财务信息已存在！";
                }
                else
                {
                    if (!Finance.UpdateFinance(r, type,pk_User))
                    {
                        code = 1;
                        msg = "添加失败";
                    }
                }

            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 根据账号ID查询
        /// </summary>
        [HttpGet]
        public JObject GetAttendanceInfo(int pk_Account)
        {
            Finance_Account Attend = new Finance_Account();
            if (pk_Account > 0)
            {
                Attend = Finance.GetFinanceInfo(pk_Account);
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attend}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 根据财务ID查询
        /// </summary>
        [HttpGet]
        public JObject GetFinanceInfo(int pk_Finance)
        {
            Finance_List Attend = new Finance_List();
            if (pk_Finance > 0)
            {
                Attend = Finance.GetFinancelinsInfo(pk_Finance);
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attend}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取Finance_Account类型数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetTypeList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetTypeList()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取Finance_Account状态数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetStatusList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetStatusList()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 获取Finance_List类型数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetTypeList2()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetTypeList2()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取Finance_List状态数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetStatusList2()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetStatusList2()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 获取Finance_List类型数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetTypeList3()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetTypeList3()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取Finance_List状态数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetStatusList3()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetStatusList3()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }


        /// <summary>
        /// 根据FullName查询Finance_Account表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject FNameSelectFinanceList(int page, int limit, string AccountName)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Finance.FNameSelectFinanceList(page,limit,AccountName) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 根据FullName查询Finance_List表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject FNameSelectFinance(int page, int limit, string Title)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Finance.FNameSelectFinance(page,limit,Title) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 删除账号管理
        /// </summary>
        /// <param name="pk_Name"></param>
        /// <returns></returns>
        [HttpPost]
        public JObject DelAccountdata()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_Account = int.Parse(RequestForm["pk_Account"].ToString());
            Finance_Account r = Finance.GetFinanceInfo(pk_Account);
            msg = "删除成功";
            if (r != null)
            {
                if (!Finance.DelAccountdata(pk_Account))
                {
                    //失败
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "薪资不存在";
            }

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 删除财务信息
        /// </summary>
        /// <param name="pk_Name"></param>
        /// <returns></returns>
        [HttpPost]
        public JObject DelFinancedata()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_Finance = int.Parse(RequestForm["pk_Finance"].ToString());
            Finance_List r = Finance.GetFinancelinsInfo(pk_Finance);
            msg = "删除成功";
            if (r != null)
            {
                if (!Finance.DelFinancedata(pk_Finance))
                {
                    //失败
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "财务不存在";
            }

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #region 财务报表
        /// <summary>
        /// 收入
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject FinanceIncome()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.FinanceIncome() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        
        /// <summary>
        /// 支出
        /// </summary>
        /// <returns></returns>
       [HttpGet]
        public JObject FinancePay()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.FinancePay() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 待定
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject Financeindeterminate()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.Financeindeterminate() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取盈利数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetChartList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.GetFinceInfo()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 支出收入详细报表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject ReportList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Finance.ReportList()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion
    }
}
